package graphics.fingerprint;

import command.Constants;
import command.SimpleTouchCommand;

import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

public class MarkListener implements OnTouchListener {
	private CommandPusher pusher;
	private UserController controller;
	private GestureDetector gd;

	public MarkListener(UserController userController, GestureDetector gd) {
		this.controller = userController;
		this.gd = gd;
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		float x = event.getX();
		float y = event.getY();
		PaintView view = (PaintView) v;
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			Log.d("Debug&Info", "*****************");
			if (this.controller.getPointer() == UserController.POINTER_PEN)
				view.touch_start(x, y);
			fingerPressed(x, y);
			Log.d("Debug&Info", "Action Down");
			break;
		case MotionEvent.ACTION_MOVE:
			if (this.controller.getPointer() == UserController.POINTER_PEN)
				view.touch_move(x, y);
			fingerMoved(x, y);
			Log.d("Debug&Info", "Action move");
			break;
		case MotionEvent.ACTION_UP:
			if (this.controller.getPointer() == UserController.POINTER_PEN)
				view.touch_up();
			if (this.controller.getPointer() != UserController.POINTER_ARROW)
				fingerReleased(x, y);
			Log.d("Debug&Info", "Action Up");
			break;
		}
		view.invalidate();
		return gd.onTouchEvent(event);
	}

	private void fingerMoved(float x, float y) {
		SimpleTouchCommand sac = new SimpleTouchCommand(
				SimpleTouchCommand.TouchType.MOVE, Constants.BUTTON1_MASK, x
						- this.controller.getImage_x(), y
						- this.controller.getImage_y());
		pusher.pushCommand(sac);
	}

	private void fingerReleased(float x, float y) {
		SimpleTouchCommand sac = new SimpleTouchCommand(
				SimpleTouchCommand.TouchType.RELEASE, Constants.BUTTON1_MASK, x
						- this.controller.getImage_x(), y
						- this.controller.getImage_y());
		pusher.pushCommand(sac);
	}

	private void fingerPressed(float x, float y) {
		SimpleTouchCommand sac = new SimpleTouchCommand(
				SimpleTouchCommand.TouchType.PRESS, Constants.BUTTON1_MASK, x
						- this.controller.getImage_x(), y
						- this.controller.getImage_y());
		pusher.pushCommand(sac);
	}

	public void setPusher(CommandPusher pusher) {
		this.pusher = pusher;
	}

	public CommandPusher getPusher() {
		return pusher;
	}

}
